package com.dawan.sbjy.service.impl;

import com.dawan.sbjy.dao.BaseDao;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;
//子类里面的T是给父类里面T赋值，子类里面的T是实参，父类里面的t是形参
public class BaseDaoImpl<T> implements BaseDao<T> {

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;
    private Class type;

    public BaseDaoImpl(){
        this.type = this.getDAOClass();
    }
    @SuppressWarnings("all")
    private Class getDAOClass(){
        Class<T> entityClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return entityClass;
    }
    private String getMethodPath(String methodType){
        return type.getSimpleName() + "Mapper." + methodType;
    }
    @Override
    public void save(String sql) {
//        String pahtq=getMethodPath("save");
        sqlSessionTemplate.insert(sql);
    }
    @Override
    public void delete(T obj) {
        sqlSessionTemplate.delete(getMethodPath("delete"), obj);
    }
    @Override
    public void update(T obj) {
        sqlSessionTemplate.update(getMethodPath("update"), obj);
    }
    @Override
    public T get(Integer id) {
        return sqlSessionTemplate.selectOne(getMethodPath("get"),id);
    }

    @Override
    public List<T> list() {
        return null;
    }


}
